﻿/*
 * BitmapMap
 * 
 * initial implementation : 20.10.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Drawing;
using System.Collections.Generic;
using Petra.Gis;
using Petra.Gis.RenderingServices.PlanarBase;
using Petra.Gis.ReferenceSystemServices;

namespace Petra.Gis.RenderingServices.GDIPlus
{
  /// <summary>
  /// BitmapMap renders GIS data as <c>System.Drawing.Bitmap</c>
  /// </summary>
  public partial class BitmapMap : MapBase
  {

    protected Bitmap _outBitmap;


    #region ctor

    public BitmapMap()
      :base()
    {

      Legend = new Legend(this);
      _transMatrix = new DisplayTransformMatrix();

      //define default axes...
      // normal GDI+ graphic has origin in left top corner
      _OutputRenderingAxes = new AxisList()
      {
        {new Axis("X", Axis.OrientationType.East)},
        {new Axis("Y", Axis.OrientationType.South)}
      };
      //_OutputRenderingAxes.Add(new Axis("X", Axis.OrientationType.East));
      //_OutputRenderingAxes.Add(new Axis("Y", Axis.OrientationType.South));


      //this map is in pixels
      this.DisplayPaperUnits = MapPaperUnit.px;
    }

    


    public BitmapMap(int width, int height, ProjectedCoordinateSystem mapCrs, double nominalMapScale)
      : base(width, height, mapCrs, nominalMapScale, MapPaperUnit.px, "GridMap")
    {

      Legend = new Legend(this);
      _transMatrix = new DisplayTransformMatrix();
      
      ////init transformation
      //DisplayTransformMatrix tmpMatrix = new DisplayTransformMatrix();
      //tmpMatrix.Translate(mapCrs.DefaultEnvelope.Left, mapCrs.DefaultEnvelope.Bottom);
      //double scale = (mapCrs.DefaultEnvelope.Right - mapCrs.DefaultEnvelope.Left) / width;
      //tmpMatrix.Scale(scale, (-1) * scale);

      //_renderer = (RendererBase)new GridRenderer(this, Graphics.FromImage(_outBitmap), tmpMatrix);
    }

    #endregion ctor


    //public BitmapMapRendererBase createRenderer(System.Type geomType)
    //{

    //  throw new NotImplementedException();
    //}

    //protected override RendererBase mapCreateRenderer(System.Type geomType)
    //{

    //  return this.createRenderer(geomType);
    //}




    /// <summary>
    /// implementation must override this property
    /// </summary>
    protected override MapLegend mapLegend
    {
      get { return this.Legend; }
      set { this.Legend = (Legend)value; }
    }



    private Legend _legend;

    /// <summary>  
    /// get/set map legend. When null is supplied as input, this property throws error.
    /// </summary>
    new public Legend Legend
    {
      get { return (Legend)_legend; }
      set
      {
        if (value == null)
          throw new ArgumentException("invalid null legend");

        _legend = value;
      }
    }




    public Bitmap Draw()
    {
      refreshMapContent();
      return _outBitmap;
    }



    //protected override object mapDraw()
    //{
    //  return this.draw();
    //}

    /// <summary>
    /// implementation must override this property
    /// </summary>
    protected override RenderTransformMatrix mapRenderTransform
    {
      get { return this.RenderTransform; }
      //set { this.RenderTransform = (DisplayTransformMatrix)value; }
    }


    /// <summary>
    /// gets Map render transform matrix
    /// </summary>
    public DisplayTransformMatrix RenderTransform 
    {
      get {return (DisplayTransformMatrix)_transMatrix ;}
      //set { _transMatrix = value; }
    }



    public Graphics OutputGraphics
    {
      get 
      {
        if (_outBitmap == null || _outBitmap.Width != (int)this.Width || _outBitmap.Height != (int)this.Height)
        {
          //initialize output bitmap
          _outBitmap = new Bitmap((int)this.Width, (int)this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
          //_outBitmap.MakeTransparent(_outBitmap.GetPixel(0,0));
          //_outBitmap.MakeTransparent();
          System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(_outBitmap);
          //g.ResetTransform();
          //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
          g.Clear(System.Drawing.Color.White);

        }

        return Graphics.FromImage(_outBitmap); 
      }
    }


  }

}
